// 2013-7-31下午09:55:13  net.betou.trade.main.dao.impl.UserGroupDaoImpl.java

package net.betou.trade.main.dao.impl;

import java.util.List;

import net.betou.common.hibernate3.Finder;
import net.betou.common.hibernate3.HibernateBaseDao;
import net.betou.trade.entity.main.UserGroup;
import net.betou.trade.main.dao.UserGroupDao;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

/**
 * 
 * @author 叶装装
 */
@Repository
public class UserGroupDaoImpl extends HibernateBaseDao< UserGroup, Integer > implements UserGroupDao{

	public UserGroup deleteById( Integer id ) {
		UserGroup bean = get( id );
		if( bean != null ){
			getSession( ).delete( bean );
		}
		return bean;
	}

	public UserGroup findByDefault( ) {
		String hql = "from UserGroup where groupDefault=?";
		return ( UserGroup ) findUnique( hql, true );
	}

	public UserGroup findById( Integer id ) {
		return get( id );
	}

	@SuppressWarnings( "unchecked" )
	public List< UserGroup > getList( Boolean allowRead, String searchProperty, String searchValue, String orderProperty, String orderDirection ) {
		Finder f = Finder.create( "from UserGroup bean where 1=1" );
		if( allowRead != null ){
			f.append( " and allowRead=:read" );
			f.setParam( "read", allowRead );
		}
		
		createSearch( f, searchProperty, searchValue );
		createOrder( f, orderProperty, orderDirection, "id" );
		
		return find( f, true );
	}

	public UserGroup save( UserGroup bean ) {
		getSession( ).save( bean );
		return bean;
	}
	
	// 只有在存在default的时候，才可以进行更新
	public int updateToDefault( ) {
		String hql = "update UserGroup set groupDefault=? where groupDefault=?";
		Query query = createQuery( hql, false, true );
		return query.executeUpdate( );
	}

	@Override
	protected Class< UserGroup > getEntityClass( ) {
		return UserGroup.class;
	}

}
